<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>
    <div class="head-title">添加内容</div>
    <div class="add-area">
      <VueDraggable
        v-model="showComponentData.default_sub_list"
        :animation="150"
        ghostClass="ghost"
        class="content"
        @end="handleUpdateComponent"
      >
        <div
          v-for="(item, index) in showComponentData.default_sub_list"
          :key="item.id"
          class="carousel-item"
        >
          <div class="avatar-uploader" @click="handleChangeImage(index)">
            <el-image v-if="item.fileUrl" :src="item.fileUrl" class="avatar" fit="cover"></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"
              ><el-icon> <Plus /> </el-icon
            ></i>
            <div v-if="item.fileUrl" class="mask">替换</div>
            <el-icon
              v-if="item.fileUrl"
              class="del-image"
              :size="20"
              @click.stop="handleDelImg(index)"
              ><CircleCloseFilled
            /></el-icon>
          </div>

          <div class="column">
            <div class="link">
              <el-icon style="color: var(--el-color-primary)"><Link /></el-icon>
              <el-link type="primary" @click="handleOpenLinkDialog(index)">{{
                item.link?.name || '请选择跳转链接'
              }}</el-link></div
            >
            <p class="tip">建议图片宽度750px，高度跟随第一张图片的高度变化</p>
          </div>

          <el-icon class="del-icon" :size="20" @click="handleDel(index)"
            ><CircleCloseFilled
          /></el-icon>
        </div>

        <div>
          <!--          <el-button type="primary" @click="handleAdd" plain :icon="Plus" style="width: 100%"-->
          <!--            >添加图片</el-button-->
          <!--          >-->
          <el-tag type="primary" class="submit-btn" @click="handleAdd">添加图片</el-tag>
        </div>
      </VueDraggable>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      @close-dialog="handleSelectLink"
    ></link-list-dialog>

    <image-selected-dialog
      v-if="showImageChooseDialog"
      :single-type="selectImageSingle"
      :operation-type="false"
      :show-image-choose-dialog="showImageChooseDialog"
      @close-dialog="handleCloseImageDialog"
    ></image-selected-dialog>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'
  import { VueDraggable } from 'vue-draggable-plus'
  import { Plus, CircleCloseFilled, Link } from '@element-plus/icons-vue'
  import debounce from 'lodash/debounce'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default() {
        return {}
      }
    }
  })

  const form = reactive({
    itemStyle: {
      height: 136,
      borderRadius: 12
    }
  })

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.itemStyle = raw.itemStyle
    handleUpdateComponent()
  }, 300)

  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)
  const showLinkDataDialog = ref(false)
  const showImageChooseDialog = ref(false)
  const selectImageSingle = ref(false)
  const changeIndex = ref()

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleAdd() {
    showComponentData.value.default_sub_list.push({
      fileUrl: 'https://image.lutongjiakao.com/system/82895dd1bc8c4960b05f552446bf5c76.png'
    })
  }

  function handleChangeImage(index: number) {
    selectImageSingle.value = true
    changeIndex.value = index
    showImageChooseDialog.value = true
  }

  function handleDel(index: number) {
    showComponentData.value.default_sub_list.splice(index, 1)
    handleUpdateComponent()
  }

  function handleDelImg(index: number) {
    showComponentData.value.default_sub_list[index].fileUrl = ''
    handleUpdateComponent()
  }

  function handleCloseImageDialog(value: any) {
    if (value) {
      if (Array.isArray(value)) {
        showComponentData.value.default_sub_list.push(...value)
      } else {
        showComponentData.value.default_sub_list[changeIndex.value] = value
      }
      handleUpdateComponent()
    }
    showImageChooseDialog.value = false
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  function handleOpenLinkDialog(index: number) {
    showLinkDataDialog.value = true
    changeIndex.value = index
  }

  function handleSelectLink(value: any) {
    if (value) {
      showComponentData.value.default_sub_list[changeIndex.value].link = value
    }
    showLinkDataDialog.value = false

    handleUpdateComponent()
  }

  onMounted(() => {
    if (showComponentData.value.itemStyle.borderRadius) {
      form.itemStyle.borderRadius = showComponentData.value.itemStyle.borderRadius
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .head-title {
      padding: 20px 0;
      font-size: 14px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .add-area {
      width: 100%;

      .tip {
        font-size: 14px;
        color: #ccc;
        margin-bottom: 10px;
      }

      .content {
        .carousel-item {
          width: 100%;
          margin-bottom: 20px;
          position: relative;
          display: flex;
          align-items: center;
          background-color: #f7f8fa;
          padding: 0 10px;
          box-sizing: border-box;

          &:hover {
            .del-icon {
              display: block;
            }
          }

          .avatar-uploader {
            border: 1px dashed #d9d9d9;
            border-radius: 12px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            width: 80px;
            height: 80px;
            background-color: #fff;
            text-align: center;

            .mask {
              position: absolute;
              bottom: 0;
              left: 0;
              width: 100%;
              height: 24px;
              background-color: rgba(0, 0, 0, 0.6);
              display: flex;
              align-items: center;
              justify-content: center;
              opacity: 0;
              transition: opacity 0.3s ease;
              color: #fff;
              font-size: 12px;
            }

            .del-image {
              display: none;
              position: absolute;
              top: 0;
              right: 0;
              cursor: pointer;
              color: #8c939d;
            }

            &:hover .mask {
              opacity: 1;
            }

            &:hover .del-image {
              display: block;
            }
          }

          .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 80px;
            height: 80px;
            line-height: 80px;
            text-align: center;
          }

          .avatar {
            width: 100%;
            height: 80px;
            display: block;
          }

          .del-icon {
            display: none;
            position: absolute;
            top: -2px;
            right: -2px;
            cursor: pointer;
            color: #8c939d;
          }

          .column {
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            margin-top: 6px;
            width: 320px;
            height: 100px;
            padding-left: 10px;

            .link {
              display: flex;
              align-items: center;
              height: 40px;
              border-radius: 6px;
              background-color: #fff;
              padding-left: 10px;
            }

            .tip {
              font-size: 12px;
            }
          }
        }
      }
    }

    .submit-btn {
      width: 100%;
      height: 40px !important;
      cursor: pointer;
      font-size: 14px;
    }

    .component-style {
      width: 100%;
      box-sizing: border-box;
      margin-top: 40px;
      //padding: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 18px;
          font-weight: bold;
          color: black;
        }
      }
    }
  }
</style>
